<template>
  <div class="app-container">
    <!-- 搜索配置   -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="ip" label="ip">
          <el-input
            v-model="queryParams.ip"
            placeholder="ip"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="配置名" prop="conf_name" />
        <el-table-column label="配置key" prop="conf_key" />
        <el-table-column label="配置value" prop="conf_value" />

        <el-table-column label="操作" fixed="right" align="left" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click.stop="handleUpdate(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--配置弹窗-->
    <el-drawer
      v-model="dialog.visible"
      :title="dialog.title"
      size="30%"
      @close="handleCloseDialog"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="computedRules"
        label-width="100px"
        :inline="true"
      >
        <el-form-item label="名称" prop="conf_name">
          <el-input
            v-model="formData.conf_name"
            disabled
            placeholder="请输入名称"
          />
        </el-form-item>
        <el-form-item label="key" prop="conf_key">
          <el-input v-model="formData.conf_key" disabled placeholder="key" />
        </el-form-item>
        <el-form-item label="配置值" prop="conf_value">
          <el-input v-model="formData.conf_value" placeholder="请输入配置值" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmitClick">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-drawer>
  </div>
</template>
<script setup lang="js">
defineOptions({
  name: "Config",
  inherititems: false,
});
import { postConfig, getPage } from "@/api/config";

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  ip: "",
});

// 配置表格数据
const pageData = ref();

/** 查询 */
function handleQuery() {
  loading.value = true;
  getPage(queryParams)
    .then((data) => {
      pageData.value = data;
    })
    .finally(() => {
      loading.value = false;
    });
}

const computedRules = computed(() => {
  const rules = {
    conf_value: [
      { required: true, message: "请输入配置的值", trigger: "blur" },
    ],
  };
  return rules;
});
const dialog = reactive({
  title: "",
  visible: false,
});
const formData = ref({});
function handleUpdate(row) {
  dialog.visible = true;
  formData.value = {};
  formData.value.conf_name = row.conf_name;
  formData.value.conf_key = row.conf_key;
  formData.value.conf_value = row.conf_value;
}

function handleSubmitClick() {
  dataFormRef.value.validate((isValid) => {
    if (isValid) {
      loading.value = true;
      let d = {
        key: formData.value.conf_key,
        value: formData.value.conf_value,
      };
      postConfig(d)
        .then((data) => {
          handleQuery();
        })
        .finally(() => {
          dialog.visible = false;
        });
    }
  });
}

const dataFormRef = ref(ElForm);
/** 关闭弹窗 */
function handleCloseDialog() {
  dialog.visible = false;

  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();

  formData.value.id = undefined;
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});
</script>

<style scoped lang="scss"></style>
